package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

// CreateMindScale 创建MindScale记录
// Author [piexlmax](https://github.com/piexlmax)
func CreateMindScale(MindScale model.MindScale) (err error) {
	err = global.GVA_DB.Create(&MindScale).Error
	return err
}

// DeleteMindScale 删除MindScale记录
// Author [piexlmax](https://github.com/piexlmax)
func DeleteMindScale(MindScale model.MindScale) (err error) {
	err = global.GVA_DB.Delete(&MindScale).Error
	return err
}

// DeleteMindScaleByIds 批量删除MindScale记录
// Author [piexlmax](https://github.com/piexlmax)
func DeleteMindScaleByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.MindScale{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateMindScale 更新MindScale记录
// Author [piexlmax](https://github.com/piexlmax)
func UpdateMindScale(MindScale model.MindScale) (err error) {
	err = global.GVA_DB.Save(&MindScale).Error
	return err
}

// GetMindScale 根据id获取MindScale记录
// Author [piexlmax](https://github.com/piexlmax)
func GetMindScale(id uint) (err error, MindScale model.MindScale) {
	err = global.GVA_DB.Preload("MindScaleRes").Where("id = ?", id).First(&MindScale).Error
	return
}

// GetMindScaleInfoList 分页获取MindScale记录
// Author [piexlmax](https://github.com/piexlmax)
func GetMindScaleInfoList(info request.MindScaleSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&model.MindScale{})
	var MindScales []model.MindScale
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.Account != "" {
		db = db.Where("`account` LIKE ?", "%"+info.Account+"%")
	}
	if info.Name != "" {
		db = db.Where("`name` LIKE ?", "%"+info.Name+"%")
	}
	if info.PlateForm != 0 {
		db = db.Where("`plate_form` = ?", info.PlateForm)
	}
	if info.Sex != 0 {
		db = db.Where("`sex` = ?", info.Sex)
	}
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&MindScales).Error
	return err, MindScales, total
}
